#!/usr/bin/perl.exe -w

if(@ARGV!=3) {
	die "Please 3 file name as input Log File eg:START.log END.log tpsapos3_TAT.log \n"
}

$fileStart=$ARGV[0];
open MYFILE_START, "$fileStart" or die "$fileStart: $!\n";

$fileEnd=$ARGV[1];
open MYFILE_END, "$fileEnd" or die "$fileEnd: $!\n";

$saveFile=$ARGV[2];

open (OUTPUT,">$saveFile")||die "Can't open file $saveFile:$!\n" ;

my $startLines="";
my $endLines="";
my %ouputTATHash;
my $outputLine;

my $endMsg_1="IMS0002";
my $endMsg_2="EMS0001";
my $endMsg_3="EMS0002";

my @eclipseTimeArray=qw{0 0 0 0 0 0 0};

while ($startLines=<MYFILE_START>){
	chomp $startLines;
	my @fields=split/:/,$startLines;
	if(@fields){
		my $start_time=substr($startLines, 0, 23);
		my @eachLineArray=$start_time;
		my $sessionId=substr($fields[3],0,32);
		$ouputTATHash{$sessionId}=[@eachLineArray];
	}
}

while ($endLines=<MYFILE_END>){
	chomp $endLines;
	my @fields=split/:/,$endLines;
	my $sessionId=substr($fields[3],0,32);
	if(exists $ouputTATHash{$sessionId}){
		my $end_time=substr($endLines, 0, 23);
		@outputLineArray=@{$ouputTATHash{$sessionId}};
		$outputLineArray[1]=$end_time;
		
		if(index($endLines,$endMsg_1)!=-1){
			$outputLineArray[2]=$endMsg_1;
		}elsif(index($endLines,$endMsg_2)!=-1){
			$outputLineArray[2]=$endMsg_2;
		}else{
			$outputLineArray[2]=$endMsg_3;
		}
		$ouputTATHash{$sessionId}=[@outputLineArray];
	}
}



foreach my $key ( keys %ouputTATHash )
{
    my $value = $ouputTATHash{$key};
    
	my $outputLine=$key;
	
	my $TATResult=-1;
	
	$outputLine.="\t";
	

	if(defined($$value[0])){
		$outputLine.=$$value[0];
	}
	
    $outputLine.="\t";
	if(defined($$value[1])){
		$outputLine.=$$value[1];
	}
	
	$outputLine.="\t";
	if(defined($$value[0])&&defined($$value[1])){
		$TATResult=&TAT($$value[0],$$value[1]);
		$outputLine.=$TATResult;
	}else{
		$outputLine.="#N/A";
	}
	
	
    $outputLine.="\t";
	if(defined($$value[2])){
		$outputLine.=$$value[2];
	}
	
	$outputLine.="\n";
	
	if($TATResult<=10.0&&$TATResult>=0.0){
		$eclipseTimeArray[0]=$eclipseTimeArray[0]+1;
	}elsif($TATResult<=20.0&&$TATResult>10.0){
		$eclipseTimeArray[1]=$eclipseTimeArray[1]+1;
	}elsif($TATResult<=30.0&&$TATResult>20.0){
		$eclipseTimeArray[2]=$eclipseTimeArray[2]+1;
	}elsif($TATResult<=40.0&&$TATResult>30.0){
		$eclipseTimeArray[3]=$eclipseTimeArray[3]+1;
	}elsif($TATResult<=50.0&&$TATResult>40.0){
		$eclipseTimeArray[4]=$eclipseTimeArray[4]+1;
	}elsif($TATResult>50.0){
		$eclipseTimeArray[5]=$eclipseTimeArray[5]+1;
	}else{
		$eclipseTimeArray[6]=$eclipseTimeArray[6]+1;
	}

	print OUTPUT $outputLine;
}

$outputLine="";
foreach $elem(@eclipseTimeArray){
	$outputLine.=$elem.="\n";
}

print OUTPUT $outputLine;

sub TAT{
	my ($startDatetime,$endDatetime);
	($startDatetime,$endDatetime)=@_;
	my ($yearStart,$monStart,$mdayStart,$hourStart,$minStart,$secStart);
	my ($yearEnd,$monEnd,$mdayEnd,$hourEnd,$minEnd,$secEnd);
    ($yearStart,$monStart,$mdayStart,$hourStart,$minStart,$secStart)=split/\/| |:/,$startDatetime;	
    ($yearEnd,$monEnd,$mdayEnd,$hourEnd,$minEnd,$secEnd)=split/\/| |:/,$endDatetime;
	my $startDateTimeMillSec=$hourStart*3600*1000+$minStart*60*1000+$secStart*1000;
	my $endDateTimeMillSec=$hourEnd*3600*1000+$minEnd*60*1000+$secEnd*1000;
	my $TAT=($endDateTimeMillSec-$startDateTimeMillSec)/1000.0;
	my $TATRounded=sprintf("%.4f",$TAT);
	$TATRounded;
}

close MYFILE_START;
close MYFILE_END;
close OUTPUT;